<?php
class pk_friend extends BaseModel implements Retrievable {

	var $user;
	var $friend;
	var $status;
	
    public function pk_friend($values) {
        parent::__construct($values);
    }

    public function validate() {
        $errors = Array();
		return (count($errors) === 0 ? true : $errors);
    }

    public static function get($id, $primaryKey='id') {
        return parent::get($id, __CLASS__, $primaryKey);
    }
    
    public static function getAll($searchArray=Array(), $sortArray=Array(), $limit=0) {
        return parent::getAll($searchArray, $sortArray, __CLASS__, $limit);
    }
    
    public function exists() {
    	return ($this->id > 0);
    }
    
    public function confirm() {
    	$this->status = '1';
    	$this->update();
    }
    
    public static function getFriends($user) {
    	/*
    	$friends = array();
    	$as_inviter = pk_friend::getAll(array("user"=>$user, "status"=>"1"));
    	foreach($as_inviter as $e) {
    		$friends[$e->id] = $e;
    	}    	
    	$as_invitee = pk_friend::getAll(array("friend"=>$user, "status"=>"1"));
    	foreach($as_invitee as $e) {
    		$friends[$e->id] = $e;
    	}
    	krsort($friends);
    	$friends = array();
    	return $friends;
    	*/
    	$query = "SELECT * FROM pk_friend WHERE (user=$user OR friend=$user) AND status=1 ORDER BY id DESC";
    	$fs = BaseModel::getByQuery($query, __CLASS__);
    	return $fs;
    }
    
}
?>